﻿package net.wangit.view.enumeration;

import java.util.HashMap;
import java.util.Map;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.framework.util.DataPrivilegeUtil;
import net.wangit.support.MapListFactory;
import net.wangit.util.Checker;
import net.wangit.util.ClassUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class EnumerationFactory {

	private static Log log = LogFactory
			.getLog(net.wangit.view.enumeration.EnumerationFactory.class);

	public EnumerationFactory() {
	}

	public static synchronized MapList getEnumeration(ActionContext ac,
			String domain, String eid) {
		if (Checker.isEmpty(domain) || Checker.isEmpty(eid)) {
			log.error((new StringBuilder(
					"empty arguments is not acceptable: domain=")).append(
					domain).append(" eid=").append(eid).toString());
			return null;
		} else {
			eid = eid.toLowerCase();
			MapList items = createEnumeration(domain, eid);
			return EnumerationHelper.resolve(ac, items);
		}
	}

	private static MapList createEnumeration(String domain, String eid) {
		EnumerationConfig config = EnumerationConfigFactory
				.getEnumerationConfig(domain, eid);
		int dataMode = config.getDataMode();
		MapList items;
		if (dataMode == 1) {
			items = config.getList();
		} else {
			items = EnumerationCache.get(domain, eid);
			if (items == null) {
				if (dataMode == 2)
					items = EnumerationDataUtil.createEnumeration(domain,
							config);
				else if (dataMode == 3) {
					String factoryName = config.getDataExpression();
					try {
						MapListFactory factory = (MapListFactory) ClassUtil
								.loadClass(factoryName).newInstance();
						items = factory.getMapList();
					} catch (Exception e) {
						throw new EnumerationConfigurationException(
								(new StringBuilder(
										"could not get Enumeration from:"))
										.append(factoryName).toString(), e);
					}
				}
				items = insertRow(items, config.getList());
				if (config.isCacheEnabled())
					EnumerationCache.put(domain, eid, items);
			}
		}
		if (config.hasDataPrivilege())
			DataPrivilegeUtil.filterEnumeration(config.getDomain(), config
					.getId(), items);
		return items;
	}

	private static MapList insertRow(MapList items, MapList list) {
		if (Checker.isEmpty(items))
			return list;
		if (!Checker.isEmpty(list)) {
			Map temp = new HashMap();
			int beforeIndex = 0;
			for (int i = 0; i < list.size(); i++) {
				Row row = list.getRow(i);
				int id = list.getRow(i).getInt("id", 0);
				String value = row.get("value");
				String name = row.get("name");
				if (id < 0) {
					temp.put("value", value);
					temp.put("name", name);
					items.insertRow(beforeIndex++, temp);
				} else {
					int size = items.size();
					items.put(size, "value", value);
					items.put(size, "name", name);
				}
			}

		}
		return items;
	}

}
